package b.i.d;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.graphics.Typeface.Builder;
import android.graphics.fonts.FontVariationAxis;
import android.net.Uri;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.util.Log;
import b.f.f;
import b.i.c.b.c;
import b.i.c.b.d;
import b.i.h.e;
import c.b.a.a.a;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/* compiled from: TypefaceCompatApi26Impl */
public class g extends e {
    public final Class<?> g;
    public final Constructor<?> h;
    public final Method i;
    public final Method j;
    public final Method k;
    public final Method l;
    public final Method m;

    public g() {
        Constructor constructor;
        Method n;
        Method o;
        Method method;
        Method method2;
        Method method3;
        Class cls = null;
        try {
            Class cls2 = Class.forName("android.graphics.FontFamily");
            constructor = cls2.getConstructor(new Class[0]);
            n = n(cls2);
            o = o(cls2);
            method = cls2.getMethod("freeze", new Class[0]);
            method2 = cls2.getMethod("abortCreation", new Class[0]);
            cls = p(cls2);
            Class cls3 = cls2;
            method3 = cls;
            cls = cls3;
        } catch (ClassNotFoundException | NoSuchMethodException e) {
            StringBuilder g = a.g("Unable to collect necessary methods for class ");
            g.append(e.getClass().getName());
            Log.e("TypefaceCompatApi26Impl", g.toString(), e);
            method3 = cls;
            method2 = method3;
            constructor = method2;
            n = constructor;
            o = n;
            method = o;
        }
        this.g = cls;
        this.h = constructor;
        this.i = n;
        this.j = o;
        this.k = method;
        this.l = method2;
        this.m = method3;
    }

    private Object m() {
        try {
            return this.h.newInstance(new Object[0]);
        } catch (IllegalAccessException | InstantiationException | InvocationTargetException unused) {
            return null;
        }
    }

    public Typeface a(Context context, c cVar, Resources resources, int i) {
        if (!l()) {
            return super.a(context, cVar, resources, i);
        }
        Object m = m();
        if (m == null) {
            return null;
        }
        d[] dVarArr = cVar.a;
        int length = dVarArr.length;
        int i2 = 0;
        while (i2 < length) {
            d dVar = dVarArr[i2];
            if (i(context, m, dVar.a, dVar.e, dVar.b, dVar.c, FontVariationAxis.fromFontVariationSettings(dVar.d))) {
                i2++;
            } else {
                h(m);
                return null;
            }
        }
        if (k(m)) {
            return j(m);
        }
        return null;
    }

    public Typeface b(Context context, CancellationSignal cancellationSignal, e.c[] cVarArr, int i) {
        if (cVarArr.length < 1) {
            return null;
        }
        if (l()) {
            int i2;
            f fVar = e.a;
            HashMap hashMap = new HashMap();
            for (e.c cVar : cVarArr) {
                if (cVar.e == 0) {
                    Uri uri = cVar.a;
                    if (!hashMap.containsKey(uri)) {
                        hashMap.put(uri, b.i.b.f.F0(context, cancellationSignal, uri));
                    }
                }
            }
            Map unmodifiableMap = Collections.unmodifiableMap(hashMap);
            Object m = m();
            if (m == null) {
                return null;
            }
            Object obj = null;
            for (e.c cVar2 : cVarArr) {
                if (((ByteBuffer) unmodifiableMap.get(cVar2.a)) != null) {
                    boolean booleanValue;
                    i2 = cVar2.b;
                    int i3 = cVar2.c;
                    boolean z = cVar2.d;
                    try {
                        booleanValue = ((Boolean) this.j.invoke(m, new Object[]{r7, Integer.valueOf(i2), null, Integer.valueOf(i3), Integer.valueOf(z)})).booleanValue();
                    } catch (IllegalAccessException | InvocationTargetException unused) {
                        booleanValue = false;
                    }
                    if (booleanValue) {
                        obj = 1;
                    } else {
                        h(m);
                        return null;
                    }
                }
            }
            if (obj == null) {
                h(m);
                return null;
            } else if (!k(m)) {
                return null;
            } else {
                Typeface j = j(m);
                if (j == null) {
                    return null;
                }
                return Typeface.create(j, i);
            }
        }
        e.c cVar3 = (e.c) k.e(cVarArr, i, new j(this));
        ParcelFileDescriptor openFileDescriptor;
        try {
            openFileDescriptor = context.getContentResolver().openFileDescriptor(cVar3.a, "r", cancellationSignal);
            if (openFileDescriptor == null) {
                if (openFileDescriptor != null) {
                    openFileDescriptor.close();
                }
                return null;
            }
            Typeface build = new Builder(openFileDescriptor.getFileDescriptor()).setWeight(cVar3.c).setItalic(cVar3.d).build();
            openFileDescriptor.close();
            return build;
        } catch (IOException unused2) {
            return null;
        } catch (Throwable th) {
            th.addSuppressed(th);
        }
    }

    public Typeface d(Context context, Resources resources, int i, String str, int i2) {
        if (!l()) {
            return super.d(context, resources, i, str, i2);
        }
        Object m = m();
        if (m == null) {
            return null;
        }
        if (!i(context, m, str, 0, -1, -1, null)) {
            h(m);
            return null;
        } else if (k(m)) {
            return j(m);
        } else {
            return null;
        }
    }

    public final void h(Object obj) {
        try {
            this.l.invoke(obj, new Object[0]);
        } catch (IllegalAccessException | InvocationTargetException unused) {
        }
    }

    public final boolean i(Context context, Object obj, String str, int i, int i2, int i3, FontVariationAxis[] fontVariationAxisArr) {
        try {
            return ((Boolean) this.i.invoke(obj, new Object[]{context.getAssets(), str, Integer.valueOf(0), Boolean.FALSE, Integer.valueOf(i), Integer.valueOf(i2), Integer.valueOf(i3), fontVariationAxisArr})).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    public Typeface j(Object obj) {
        try {
            Array.set(Array.newInstance(this.g, 1), 0, obj);
            return (Typeface) this.m.invoke(null, new Object[]{r1, Integer.valueOf(-1), Integer.valueOf(-1)});
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return null;
        }
    }

    public final boolean k(Object obj) {
        try {
            return ((Boolean) this.k.invoke(obj, new Object[0])).booleanValue();
        } catch (IllegalAccessException | InvocationTargetException unused) {
            return false;
        }
    }

    public final boolean l() {
        if (this.i == null) {
            Log.w("TypefaceCompatApi26Impl", "Unable to collect necessary private methods. Fallback to legacy implementation.");
        }
        return this.i != null;
    }

    public Method n(Class<?> cls) throws NoSuchMethodException {
        r0 = new Class[8];
        Class cls2 = Integer.TYPE;
        r0[2] = cls2;
        r0[3] = Boolean.TYPE;
        r0[4] = cls2;
        r0[5] = cls2;
        r0[6] = cls2;
        r0[7] = FontVariationAxis[].class;
        return cls.getMethod("addFontFromAssetManager", r0);
    }

    public Method o(Class<?> cls) throws NoSuchMethodException {
        r0 = new Class[5];
        Class cls2 = Integer.TYPE;
        r0[1] = cls2;
        r0[2] = FontVariationAxis[].class;
        r0[3] = cls2;
        r0[4] = cls2;
        return cls.getMethod("addFontFromBuffer", r0);
    }

    public Method p(Class<?> cls) throws NoSuchMethodException {
        r2 = new Class[3];
        Class cls2 = Integer.TYPE;
        r2[1] = cls2;
        r2[2] = cls2;
        Method declaredMethod = Typeface.class.getDeclaredMethod("createFromFamiliesWithDefault", r2);
        declaredMethod.setAccessible(true);
        return declaredMethod;
    }
}
